﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLSX.BO;
using QLSX.Common;

namespace QLSX
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void btnTinhThanhPhan_Click(object sender, EventArgs e)
        {
            //ThanhPhan duong = this.SelectThanhPhanByThanhPhanName(Constant.ThanhPhan.DUONG);
            //ThanhPhan bot = this.SelectThanhPhanByThanhPhanName(Constant.ThanhPhan.BOT);
            //ThanhPhan nuoc = this.SelectThanhPhanByThanhPhanName(Constant.ThanhPhan.NUOC);

            ThanhPhan duong = this.SelectThanhPhanByThanhPhanId((int)Constant.MaThanhPhan.DUONG);
            ThanhPhan bot = this.SelectThanhPhanByThanhPhanId((int)Constant.MaThanhPhan.BOT);
            ThanhPhan nuoc = this.SelectThanhPhanByThanhPhanId((int)Constant.MaThanhPhan.NUOC);

            int soLuong = 0;
            if(int.TryParse(txtSoLuong.Text, out soLuong))
            {
                lblSoLuongDuong.Text = (soLuong * duong.GiaTriMacDinh).ToString();
                lblDonViDuong.Text = duong.DonVi;

                lblSoLuongBot.Text = (soLuong * bot.GiaTriMacDinh).ToString();
                lblDonViBot.Text = bot.DonVi;

                lblSoLuongNuoc.Text = (soLuong * nuoc.GiaTriMacDinh).ToString();
                lblDonViNuoc.Text = nuoc.DonVi;
            }
        }

        private ThanhPhan SelectThanhPhanByThanhPhanName(string name)
        {
            return ThanhPhan.SelectThanhPhanByName(name);
        }
        
        private ThanhPhan SelectThanhPhanByThanhPhanId(int id)
        {
            return ThanhPhan.SelectThanhPhanById(id);
        }
        //private int SoLuongDuong(int soLuong)
        //{
        //    //return                 
        //}

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                SanPham sp = new SanPham();
                sp.TenSanPham = txtTenSanPham.Text;
                sp.SoLuong = int.Parse(txtSoLuong.Text);
                sp.SoLuongThucTe = 0;
                sp.NgaySanXuat = DateTime.Now;
                sp.NgayCapNhatSoLuong = DateTime.Now;
                sp.Duong = Double.Parse(lblSoLuongDuong.Text);
                sp.Bot = Double.Parse(lblSoLuongBot.Text);
                sp.Nuoc = Double.Parse(lblSoLuongNuoc.Text);

                SanPham.AddSanPham(sp);

                this.LoadGrid();
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.LoadGrid();
        }

        private void LoadGrid()
        {
            List<SanPham> listSanPham = SanPham.SelectAllSanPham();
            dataGridView1.Rows.Clear();
            if (listSanPham != null && listSanPham.Count > 0)
            {
                dataGridView1.Rows.Add(listSanPham.Count);
                int count = 0;
                listSanPham.ForEach(x =>
                {
                    this.dataGridView1.Rows[count].Cells["SanPhamId"].Value = x.SanPhamId;
                    this.dataGridView1.Rows[count].Cells["STT"].Value = x.Stt;
                    this.dataGridView1.Rows[count].Cells["TenSanPham"].Value = x.TenSanPham;
                    this.dataGridView1.Rows[count].Cells["SoLuong"].Value = x.SoLuong;
                    this.dataGridView1.Rows[count].Cells["SoLuongThucTe"].Value = x.SoLuongThucTe;
                    this.dataGridView1.Rows[count].Cells["NgaySanXuat"].Value = x.NgaySanXuat.ToShortDateString();
                    this.dataGridView1.Rows[count].Cells["NgayCapNhatSoLuong"].Value = x.NgayCapNhatSoLuong.ToShortDateString();
                    this.dataGridView1.Rows[count].Cells["Duong"].Value = x.Duong;
                    this.dataGridView1.Rows[count].Cells["Bot"].Value = x.Bot;
                    this.dataGridView1.Rows[count].Cells["Nuoc"].Value = x.Nuoc;

                    if (x.SoLuongThucTe == 0)
                    {
                        this.dataGridView1.Rows[count].DefaultCellStyle.BackColor = Color.Bisque;
                    }
                    count++;
                });
                this.dataGridView1.Refresh();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnChinhSuaSoLuongThucTe_Click(object sender, EventArgs e)
        {
            
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            new frmCauHinh().Show();
        }
    }
}
